import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import {
    getCookie,
    setCookie
} from '@/common/js/cookie';
import {
    Message,
    MessageBox
} from 'element-ui';
import {
    _getUrlKey
} from "@/utils/common.js"
import {
    setSessionStorage,
    getSessionStorage,
    removeSessionStorage
} from "@/common/js/storage";
import Watermark from '@/common/js/watermark'; //加水印

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 客户登录
const component = {
    login: r => require.ensure([], () => r(require('@/views/login')), 'login'),
    loginForzjhc: r => require.ensure([], () => r(require('@/views/loginForzjhc')), 'loginForzjhc'),
    layout: r => require.ensure([], () => r(require('@/components/layout')), 'layout'),
    virtualLogin: r => require.ensure([], () => r(require('@/views/budget/virtualLogin')), 'virtualLogin'), // 虚拟登录
    iseeVirtualLogin: r => require.ensure([], () => r(require('@/views/budget/iseeVirtualLogin')), 'iseeVirtualLogin'),
    fullLayout: r => require.ensure([], () => r(require('@/components/fullLayout')), 'fullLayout'),
    displayBoard: r => require.ensure([], () => r(require('@/views/budget/displayBoard')), 'displayBoard'),
    hint:  r => require.ensure([], () => r(require('@/components/login/hint')), 'hint'),
};


Vue.use(Router);
const routers = [{
        path: '',
        redirect: '/login'
    },
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/budget',
        name: 'budget',
        component: component.layout,
        children: []
    },
    {
        path: '/dashboard',
        redirect: '/dashboard/index',
        component: component.fullLayout,
        children: [
            {
                path: 'index',
                name: 'displayBoard',
                component: component.displayBoard,
                meta: {
                    title: '重要指标可视化'
                }
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: component.login,
        meta: {
            title: '登录',
            requireLogin: false
        }
    },
    {
        path: '/loginForzjhc',
        name: 'loginForzjhc',
        component: component.loginForzjhc,
        meta: {
            title: '登录',
            requireLogin: false
        }
    },
    {
        path: '/virtualLogin',
        name: 'virtualLogin',
        component: component.virtualLogin,
        meta: {
            title: '登录',
            requireLogin: false
        }
    },
    {
        path: '/iseeVirtualLogin',
        name: 'iseeVirtualLogin',
        component: component.iseeVirtualLogin,
        meta: {
            title: 'isee登录',
            requireLogin: false
        }
    },
    {
        path: '/hint',
        name: 'hint',
        component: component.hint,
        meta: {
            title: '4A管理员',
            requireLogin: false
        }
    }
    // { path: '*', redirect: '/budget/home'} // 404 统一跳首页
];

function pushRouter(name, thisArray) {
    for (var i in routers) {
        if (routers[i].name == name) {
            for (var j in thisArray) {
                routers[i].children.push(thisArray[j]);
            }
        }
    }
}

// 客户登录 -- 引入模块路由添加进路由
import budget from './budget'
pushRouter('budget', budget);
const router = new Router({
    // mode: 'history',
    scrollBehavior: () => ({
        y: 0
    }),
    routes: routers
});
// const accountInfo = {
//     "areaId": "1",
//     "areaLvl": 2,
//     "areaName": "浙江",
//     "roleId": 1,
//     "waterMark": "王树志:13306814207"
// }
// setCookie('token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIn0.rXoGO6aBp_UhPOi0-ld6YxWXWT894SOhb0Dk7oCYG10')
// window.sessionStorage.setItem('SysUserCode', 13306814207)
// window.sessionStorage.setItem('accountInfo', JSON.stringify(accountInfo))
// window.sessionStorage.setItem('userName', '王树志')
// 路由跳转之前
router.beforeEach((to, from, next) => {
    // console.log(to.meta.requireLogin)
    // 显示loading过渡效果
    // store.dispatch("ChangeLoading", true);
    // if (to.meta.requireLogin) {
    //     if (getCookie('token')) {
    //         next();
    //     } else {
    //         next('/login');
    //     }
    // } else {
    //     next();
    // }


    let token = getCookie('token');
    NProgress.start()
    if (to.meta.requireLogin) {
        if (getCookie('token')) {
            next();
        } else if (!token && to.path == '/virtualLogin') {
            router.push({
                path: '/virtualLogin'
            });
        } else if (!token && to.path != '/login') {
            Message({
                type: 'error',
                message: '登录失效，请重新登录',
            })
            router.push({
                path: '/login'
            });
        }
    } else {
        next();
    }
    // 更新页面title
    window.document.title = to.meta.title ? `${to.meta.title}--浙江电信经营预算管理系统` : '浙江电信经营预算管理系统'
});


// 路由跳转之后
router.afterEach(function (to) {
    // 关闭loading过渡效果
    if (getSessionStorage('accountInfo')) {
        Watermark.set(JSON.parse(getSessionStorage('accountInfo')).waterMark);
    }
    NProgress.done()
    // store.dispatch("ChangeLoading", false);
});
export default router;
